set(USE_GUI FALSE)

unset(TARGET_SOURCE_DIRS)
unset(TARGET_INCLUDE_DIRS)
unset(TARGET_LIB_LIST)
unset(TARGET_DEFINITIONS_LIST)

set(BUILD_INCLUDE_DIRECTORY "${CMAKE_BINARY_DIR}/include")
file(MAKE_DIRECTORY ${BUILD_INCLUDE_DIRECTORY})

# liteapp project directory
set(LITE_APP_DIRECTORY d:/workspace/cmake-test)

# application
list(APPEND TARGET_SOURCE_DIRS ${LITE_APP_DIRECTORY}/src/Application)
list(APPEND TARGET_INCLUDE_DIRS ${LITE_APP_DIRECTORY}/src/Application)
file(MAKE_DIRECTORY ${BUILD_INCLUDE_DIRECTORY}/Application)
file(GLOB BUILD_HEADER_FILES "${LITE_APP_DIRECTORY}/src/Application/*.h")
file(COPY ${BUILD_HEADER_FILES} DESTINATION ${BUILD_INCLUDE_DIRECTORY}/Application)

# platform application
list(APPEND TARGET_SOURCE_DIRS ${LITE_APP_DIRECTORY}/src/Platform/EspApplication)
list(APPEND TARGET_SOURCE_DIRS ${LITE_APP_DIRECTORY}/app/BlockScript)
list(APPEND TARGET_SOURCE_DIRS ${LITE_APP_DIRECTORY}/app/BlockScript/ComponentIDE)
list(APPEND TARGET_INCLUDE_DIRS ${LITE_APP_DIRECTORY}/src/Platform)
list(APPEND TARGET_INCLUDE_DIRS ${LITE_APP_DIRECTORY}/src/Platform/EspApplication)
list(APPEND TARGET_INCLUDE_DIRS ${LITE_APP_DIRECTORY}/app/BlockScript)
list(APPEND TARGET_INCLUDE_DIRS ${LITE_APP_DIRECTORY}/app/BlockScript/ComponentIDE)
file(GLOB BUILD_HEADER_FILES "${LITE_APP_DIRECTORY}/src/Platform/*.h")
file(COPY ${BUILD_HEADER_FILES} DESTINATION ${BUILD_INCLUDE_DIRECTORY}/Application)
file(GLOB BUILD_HEADER_FILES "${LITE_APP_DIRECTORY}/src/Platform/EspApplication/*.h")
file(COPY ${BUILD_HEADER_FILES} DESTINATION ${BUILD_INCLUDE_DIRECTORY}/Application)
file(GLOB BUILD_HEADER_FILES "${LITE_APP_DIRECTORY}/app/BlockScript/*.h")
file(COPY ${BUILD_HEADER_FILES} DESTINATION ${BUILD_INCLUDE_DIRECTORY}/Application)

# utility
list(APPEND TARGET_SOURCE_DIRS ${LITE_APP_DIRECTORY}/src/Utility)
list(APPEND TARGET_INCLUDE_DIRS ${LITE_APP_DIRECTORY}/src/Utility)
# utility libs and definitions
list(APPEND TARGET_LIB_LIST "libzip" "zlib")
list(APPEND TARGET_DEFINITIONS_LIST USE_LIBZIP USE_ZLIB)
file(MAKE_DIRECTORY ${BUILD_INCLUDE_DIRECTORY}/Utility)
file(GLOB BUILD_HEADER_FILES "${LITE_APP_DIRECTORY}/src/Utility/*.h")
file(COPY ${BUILD_HEADER_FILES} DESTINATION ${BUILD_INCLUDE_DIRECTORY}/Utility)

# gui
list(APPEND TARGET_SOURCE_DIRS ${LITE_APP_DIRECTORY}/src/GUI)
list(APPEND TARGET_SOURCE_DIRS ${LITE_APP_DIRECTORY}/src/GUI/Text)
list(APPEND TARGET_SOURCE_DIRS ${LITE_APP_DIRECTORY}/src/GUI/Element)
list(APPEND TARGET_INCLUDE_DIRS ${LITE_APP_DIRECTORY}/src/GUI)
list(APPEND TARGET_INCLUDE_DIRS ${LITE_APP_DIRECTORY}/src/GUI/Interface)
list(APPEND TARGET_INCLUDE_DIRS ${LITE_APP_DIRECTORY}/src/GUI/Text)
list(APPEND TARGET_INCLUDE_DIRS ${LITE_APP_DIRECTORY}/src/GUI/Element)
file(MAKE_DIRECTORY ${BUILD_INCLUDE_DIRECTORY}/GUI)
file(GLOB BUILD_HEADER_FILES "${LITE_APP_DIRECTORY}/src/GUI/*.h")
file(COPY ${BUILD_HEADER_FILES} DESTINATION ${BUILD_INCLUDE_DIRECTORY}/GUI)
file(MAKE_DIRECTORY ${BUILD_INCLUDE_DIRECTORY}/GUI/Text)
file(GLOB BUILD_HEADER_FILES "${LITE_APP_DIRECTORY}/src/GUI/Text/*.h")
file(COPY ${BUILD_HEADER_FILES} DESTINATION ${BUILD_INCLUDE_DIRECTORY}/GUI/Text)
file(MAKE_DIRECTORY ${BUILD_INCLUDE_DIRECTORY}/GUI/Element)
file(GLOB BUILD_HEADER_FILES "${LITE_APP_DIRECTORY}/src/GUI/Element/*.h")
file(COPY ${BUILD_HEADER_FILES} DESTINATION ${BUILD_INCLUDE_DIRECTORY}/GUI/Element)

# gui blockly
list(APPEND TARGET_SOURCE_DIRS ${LITE_APP_DIRECTORY}/src/Blockly)
list(APPEND TARGET_SOURCE_DIRS ${LITE_APP_DIRECTORY}/src/Blockly/Field)
list(APPEND TARGET_SOURCE_DIRS ${LITE_APP_DIRECTORY}/src/Blockly/FieldEditor)
list(APPEND TARGET_SOURCE_DIRS ${LITE_APP_DIRECTORY}/src/Blockly/BlockEditor)
list(APPEND TARGET_SOURCE_DIRS ${LITE_APP_DIRECTORY}/src/Blockly/BlockScript)
list(APPEND TARGET_SOURCE_DIRS ${LITE_APP_DIRECTORY}/src/Blockly/UI)
list(APPEND TARGET_INCLUDE_DIRS ${LITE_APP_DIRECTORY}/src/Blockly)
list(APPEND TARGET_INCLUDE_DIRS ${LITE_APP_DIRECTORY}/src/Blockly/Option)
list(APPEND TARGET_INCLUDE_DIRS ${LITE_APP_DIRECTORY}/src/Blockly/Field)
list(APPEND TARGET_INCLUDE_DIRS ${LITE_APP_DIRECTORY}/src/Blockly/Block)
list(APPEND TARGET_INCLUDE_DIRS ${LITE_APP_DIRECTORY}/src/Blockly/FieldEditor)
list(APPEND TARGET_INCLUDE_DIRS ${LITE_APP_DIRECTORY}/src/Blockly/BlockEditor)
list(APPEND TARGET_INCLUDE_DIRS ${LITE_APP_DIRECTORY}/src/Blockly/BlockScript)
list(APPEND TARGET_INCLUDE_DIRS ${LITE_APP_DIRECTORY}/src/Blockly/UI)

# gui libs and definitions
list(APPEND TARGET_LIB_LIST "stb")
list(APPEND TARGET_DEFINITIONS_LIST USE_GUI USE_BLOCKLY)

# javascript
list(APPEND LiteAppSrcDirs ${LITE_APP_DIRECTORY}/src/JsScript)
list(APPEND TARGET_DEFINITIONS_LIST USE_JS_SCRIPT)

# lua script
# list(APPEND TARGET_SOURCE_DIRS ${LITE_APP_DIRECTORY}/src/LuaScript)
# list(APPEND TARGET_SOURCE_DIRS ${LITE_APP_DIRECTORY}/src/LuaScript/Scripts)
# list(APPEND TARGET_LIB_LIST "Lua" "LuaBridge3")
# list(APPEND TARGET_DEFINITIONS_LIST USE_LUA)
# file(MAKE_DIRECTORY ${BUILD_INCLUDE_DIRECTORY}/LuaScript)
# file(GLOB BUILD_HEADER_FILES "${LITE_APP_DIRECTORY}/src/LuaScript/*.h")
# file(COPY ${BUILD_HEADER_FILES} DESTINATION ${BUILD_INCLUDE_DIRECTORY}/LuaScript)

# include dirs
list(APPEND TARGET_INCLUDE_DIRS ${LITE_APP_DIRECTORY}/src/Interface)
file(MAKE_DIRECTORY ${BUILD_INCLUDE_DIRECTORY}/Interface)
file(COPY ${LITE_APP_DIRECTORY}/src/Interface DESTINATION ${BUILD_INCLUDE_DIRECTORY})

# esp libs and definitions
list(APPEND TARGET_LIB_LIST "protocol_examples_common" "esp_common" "nvs_flash" "spiffs" "esp_eth" "esp_netif" "esp_wifi" "esp_http_client" "esp_http_server" "esp_https_ota" "app_update" "esp_lcd" "bt" "mbedtls")
list(APPEND TARGET_DEFINITIONS_LIST PLATFORM_ESP)

# register component
idf_component_register(SRC_DIRS ${TARGET_SOURCE_DIRS} INCLUDE_DIRS ${TARGET_INCLUDE_DIRS} REQUIRES ${TARGET_LIB_LIST})

# compile definitions
target_compile_definitions(${COMPONENT_LIB} PUBLIC ${TARGET_DEFINITIONS_LIST})
target_compile_options(${COMPONENT_LIB} PUBLIC -Wno-unused-variable -Wno-unused-but-set-parameter -Wno-ignored-qualifiers -Wno-missing-field-initializers)

# After build, copy the archive file and header file to parent example directory's main component
if (PREBUILT_DIRECTORY)
    file(COPY ${BUILD_INCLUDE_DIRECTORY}/ DESTINATION ${PREBUILT_DIRECTORY}/include/LiteApp)
	add_custom_command(TARGET ${COMPONENT_LIB} POST_BUILD COMMAND ${CMAKE_COMMAND} -E copy $<TARGET_FILE:${COMPONENT_LIB}> ${PREBUILT_DIRECTORY}/lib COMMENT "Copying built archive file to prebuilt directory...")
endif()
